<template>
  <view>
    <!-- 使用自定义的搜索组件 -->
    <!-- 不加.native修饰符就是绑定的自定义事件而不是原生事件 -->
    <my-search bgcolor="#e00000" radius="30" @click='gotoSearch'></my-search>
    <view class="scrollContainer">
      <!-- 左侧的滑动区域 -->
      <scroll-view scroll-y="true" :style="{height:wh+'px'}" class="left-scroll" @click="changeSelected">
        <view v-for="(item,index)  in cateList" :key="item.cat_id"
          :class="['left-scrollItem',index===active?'active':'']" :data-Index='index'>
          {{item.cat_name}}
        </view>
      </scroll-view>
      <!-- 右侧的滑动区域 -->
      <scroll-view scroll-y="true" :style="{height:wh+'px'}" class="right" :scroll-top="screenTop">
        <view class="cateGory2" v-for='item1 in cateLevel2' :key='item1.cat_id'>
          <view class="title">/ {{item1.cat_name}} /</view>
          <view class="main">
            <view class="mainItem" v-for='item2 in item1.children' :key='item2.cat_id'@click=gotoGoodsList(item2.cat_id)>
              <image :src="item2.cat_icon" mode="" class='image'></image>
              <text class='text'>{{item2.cat_name}}</text>
            </view>
          </view>
        </view>
      </scroll-view>
    </view>
  </view>
</template>

<script>
  import {
    reqCateList
  } from '../../api/api.js'
  export default {
    data() {
      return {
        wh: 0,
        cateList: [],
        active: 0,
        // 二级分类的列表
        cateLevel2: [],
        screenTop:0
      };
    },
    onLoad() {
      let res = uni.getSystemInfoSync();
      this.wh = res.windowHeight-60;
      this.getCateList();
    },
    methods: {
      async getCateList() {
        let res = await reqCateList();
        if (res.data.meta.status === 200) {
          this.cateList = res.data.message;
          this.cateLevel2 = this.cateList[0].children;
        } else {
          return uni.$showMsg();
        }
      },
      changeSelected(e) {
        this.active = e.target.dataset.index;
        this.cateLevel2 = this.cateList[e.target.dataset.index].children;
        this.screenTop=this.screenTop===0?1:0;
      },
      gotoGoodsList(id){
          uni.navigateTo({
            url:'/subpkg/goods_list/goods_list?cid='+id
          })
      },
      gotoSearch(){
        uni.navigateTo({
          url:'/subpkg/search/search'
        })
      }
    }
  }
</script>

<style lang="scss" scoped>
  .scrollContainer {
    display: flex;

    .left-scroll {
      width: 120px;

      .left-scrollItem {
        background-color: #efefef;
        line-height: 60px;
        text-align: center;
        font-size: 12px;

        &.active {
          background-color: #FFFFFF;
          position: relative;

          &::before {
            content: '';
            display: block;
            width: 3px;
            height: 30px;
            background-color: red;
            position: absolute;
            top: 50%;
            left: 0;
            transform: translateY(-50%);
          }
        }
      }
    }

    .right {
      background-color: #fff;
      .cateGory2 {
        .title {
          text-align: center;
          font-weight: bold;
          padding: 15px 0;
          font-size: 14px;
        }
        .main{
          display: flex;
          flex-wrap: wrap;
          
          .mainItem{
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
            margin-bottom: 10rpx;
            width: 33.33%;
            .image{
              height: 60px;
              width: 60px;
            }
            .text{
              font-size: 12px;
            }
          }
        }
      }
    }
  }
</style>
